package world0.gui;

import java.util.ArrayList;

import org.newdawn.slick.Graphics;

import world0.items.Item;

public class Container extends Widget {
			
	ArrayList<ItemSlot> itemSlotList;

	public Container(String name, int x, int y, int width, int height) {
		super(name, x, y, width, height);
		
		createFrame();
	}

	public void addItemSlot(int number) {
		int placeSizeX = width / number;
		int placeX = x + 4;
		
		itemSlotList = new ArrayList<ItemSlot>();
		for(int i = 0; i < number; i++) {
			itemSlotList.add(new ItemSlot(i, placeX, y + 4, placeSizeX - 4, height - 8));
			placeX += placeSizeX;
		}
	}
	
	@Override
	public void draw(Graphics g) {
		super.draw(g);
		
		g.drawString(name, x, y - 20);
		
		for(int i = 0; i < itemSlotList.size(); i++) {
			itemSlotList.get(i).draw(g);
		}
	}
	
	public boolean isSlotPlace(Item item, int mouseX, int mouseY) {
		boolean isSlotPlace = false;
		for(int i = 0; i < itemSlotList.size(); i++) {
			if(itemSlotList.get(i).area.contains(mouseX, mouseY)) {
				isSlotPlace = true;
			}
		}
		return isSlotPlace;
	}
	
	public int getSlotWidth(Item item) {
		int placeX = 0;
		for(int i = 0; i < itemSlotList.size(); i++) {
			if(itemSlotList.get(i).area.intersects(item.getShape())) {
				placeX  = itemSlotList.get(i).width;
			}
		}
		return placeX;
	}
	
	public int getSlotHeight(Item item) {
		int placeY = 0;
		for(int i = 0; i < itemSlotList.size(); i++) {
			if(itemSlotList.get(i).area.intersects(item.getShape())) {
				placeY  = itemSlotList.get(i).height;
			}
		}
		return placeY;
	}
	
	public int getFreeX(Item item, int mouseX, int mouseY) {
		int positionX = 0;
		for(int i = 0; i < itemSlotList.size(); i++) {
			if(itemSlotList.get(i).area.contains(mouseX, mouseY)) {
				positionX = itemSlotList.get(i).x;
				item.setWidth(itemSlotList.get(i).width);
			}
		}
		return positionX;
	}
	
	public int getFreeY(Item item, int mouseX, int mouseY) {
		int positionY = 0;
		for(int i = 0; i < itemSlotList.size(); i++) {
			if(itemSlotList.get(i).area.contains(mouseX, mouseY)) {
				positionY = itemSlotList.get(i).y;
				item.setHeight(itemSlotList.get(i).height);
			}
		}
		return positionY;
	}
	
	public int getFreeItemSlotXPosition(Item item) {
		int positionX = 30;
		for(int i = 0; i < itemSlotList.size(); i++) {
			if(itemSlotList.get(i).isFreeX()) {
				positionX = itemSlotList.get(i).getX();
				itemSlotList.get(i).setFreeX(false);
				item.setWidth(itemSlotList.get(i).width);
				item.setPositionResetX(positionX, itemSlotList.get(i).width);
				break;
			}
		}
		return positionX;
	}
	
	public int getFreeItemSlotYPosition(Item item) {
		int positionY = 0;
		for(int i = 0; i < itemSlotList.size(); i++) {
			if(itemSlotList.get(i).isFreeY()) {
				positionY = itemSlotList.get(i).getY();
				itemSlotList.get(i).setFreeY(false);
				item.setHeight(itemSlotList.get(i).height);
				item.setPositionResetY(positionY, itemSlotList.get(i).height);
				break;
			}
		}
		return positionY;
	}
}
